#include "delivery_person_module.h"
#include <iostream>

namespace TakeAwayPlatform
{
    DeliveryPersonModule::DeliveryPersonModule(DatabaseHandler& dbHandler) : dbHandler(dbHandler) {}

    bool DeliveryPersonModule::acceptOrder(int delivery_person_id, int order_id)
    {
        std::string sql = "UPDATE orders SET delivery_person_id = " + std::to_string(delivery_person_id) + ", status = 2 WHERE order_id = " + std::to_string(order_id);
        return dbHandler.update(sql);
    }

    bool DeliveryPersonModule::deliverOrder(int delivery_person_id, int order_id)
    {
        std::string sql = "UPDATE orders SET status = 3 WHERE order_id = " + std::to_string(order_id) + " AND delivery_person_id = " + std::to_string(delivery_person_id);
        return dbHandler.update(sql);
    }

    bool DeliveryPersonModule::viewOrders(int delivery_person_id, std::vector<int>& order_ids)
    {
        std::string sql = "SELECT order_id FROM orders WHERE delivery_person_id = " + std::to_string(delivery_person_id);
        Json::Value result = dbHandler.query(sql);

        for (const auto& row : result)
        {
            order_ids.push_back(row["order_id"].asInt());
        }

        return !order_ids.empty();
    }
}